home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Applications / Calculators / NumberCrunch 1.41 / Number Crunch II Demo / Writing External Codes (xCOD) / How To Write an xCOD next >
Encoding:
Text File  |  1992-09-03  |  7.3 KB  |  128 lines  |  [TEXT/NCII]

  1.   Writing External Code Resources
  2.  
  3. NumberCrunch II has the ability to execute code which has been written and compiled outside of the application with a Fortran, Pascal, C, or other compiler.  These "external codes" (or xCODs) usually perform specialized numerical tasks like fourier transforms and matrix inversions.  Many of these routines of this sort have been included with NCII; see the Library Routines folder.
  4.  
  5. If you have a good compiler and some programming experience on the Macintosh then you can write your own external codes and plug them into NCII.  This file gives an overview of how to proceed and some background information about programming on the Mac. 
  6.  
  7.  
  8.    Macintosh Resources
  9.  
  10. Macintosh applications are made up of a number of objects called "resources" in which exectable code, text, pictures, sounds, and a number of other kinds of data may be stored.  Resources come in different types, each with a unique 4-letter indentifier. A few of the common types include
  11.   CODE         the code that makes up an application, in <32k sized chunks,
  12.   TEXT         ascii text,
  13.    PICT         macintosh pictures, and
  14.   DLOG         dialog templates.
  15.  
  16. NCII external code segments are also resources, with their own types:
  17.    xCOD       NCII external code resource for the generic Mac version of NCII, 
  18.    xCO2       NCII external code resource for the MC68881 
  19.                     math co-processer version of NCII, and
  20.    sCOD       Text string describing the arguments to an xCOD or xCO2.
  21.  
  22. The easiest way to view and manipulate these resources is with a program called ResEdit, which is part of Apple's system software. Although ResEdit is not necessary for writing and installing NCII external code, it is very helpful for seeing which of these xCOD, xCO2, and sCOD resources are where, and for cutting, copying, and pasting them between files.
  23.  
  24. Each NCII routine consists of two resources: 1) the xCOD or xCO2 compiled code, and 2) the sCOD argument string.  Both  must have the same ID.  Once the xCOD is written and compiled (which will be discussed in the next section), creating the sCOD (whose text is also described further down) and installing both into NCII can be done either with ResEdit or directly from NCII, with the Load xCOD command.  If an sCOD is not found when the xCOD is loaded, NCII will ask for the string describing the arguments to the external code, and then create the sCOD itself.
  25.  
  26. If you choose to use ResEdit, here are a few tips.
  27.  • Create the sCOD by using the Create New Resource (cmd-K) command, and typing "sCOD" (case is important) in the dialog.  The window which is created shows the resource in Hex and ascii; click at the right edge of the window to type or paste the ascii text for the sCOD.
  28.  • The IDs of the xCOD and sCOD resources are changed with the Get Resource Info (cmd-I) command.  The two IDs must match.
  29.  • The names of the resources do not matter; the NCII routine's name is determined from the text in the sCOD.  
  30.  
  31.  
  32.    Compiling the xCOD (or xCO2)
  33.  
  34. The details of how to compile the xCOD will, of course, depend on which compiler is used.  Sample source code and associated files are included in this folder for Language Systems MPW FORTRAN and Symantec's THINK Pascal.  
  35.  
  36. The compiler must be able to
  37.  • Create independent resources (sometimes called "drivers"), not just applications.
  38.  • Accept pointers to SANE (Standard Apple Numeric Environment) Extended numbers as procedure arguments.  (Pointers are passed for all FORTRAN and Pascal "var" arguments.)
  39.  
  40. The xCOD may have up to 20 arguments, which may be
  41.     extended numbers, 
  42.     arrays of extended, 
  43.     or a pointer to another procedure.
  44. Procedure pointers are difficult but possible to manage in Pascal; see the sample source files.  In FORTRAN they are the norm.
  45.  
  46. For example, the source code for a short FORTRAN xCOD to add two numbers and return the result is
  47.  
  48.     subroutine xADD(a,b, zOut)
  49.     extended a,b,zOut
  50.     zOut = a+b
  51.     return
  52.     end
  53.  
  54. while in Pascal the same routine would be
  55.  
  56.     procedure xADD(var a,b, zOut:extended)
  57.     begin
  58.       zOut := a+b;
  59.     end;
  60.  
  61. More elaborate argument lists are given in the sample source files.
  62.  
  63. Each compiler uses a different mechanism for turning the source code into the desired xCOD resource.  In the THINK Pascal environment a project file (.π) file manages the overall picture.  The project type (Code Resource) and other parameters are set in dialogs.  In MPW FORTRAN, a Script file MakeXCOD (MakeXCO2) tells the MPW (Mac Programmer's Workshop) how to compile and link the source code.
  64.  
  65.  
  66.    The sCOD Argument Description String
  67.  
  68. All that is left is to tell NCII how to use the xCOD.  
  69.  
  70. The sCOD string used a Pascal-like procedure declaration, with lists of argument names followed by ": type" where "type" is one of the types given in the table in the section of the on-line help which explains xCOD's.  A comment describing the xCOD usually follows the argument list. The argument types are:
  71.  
  72.    declared type            NCII passed object
  73.  
  74.   real or extended         real number, array, or n-array
  75.   number or num            real or complex number, array, or n-array
  76.   RealArray                  real array or n-array
  77.   Complex                     complex number, array, or n-array 
  78.   ComplexArray            complex array or n-array
  79.   Array                         real or complex array or n-array
  80.   Prog or Program       xCOD name or NCII user program 
  81.       or Proc or Procedure
  82.   xCOD                           xCOD
  83.  
  84. With these types, an sCOD for either of the xADD procedures listed above might be
  85.  
  86.    xCOD xADD(a, b, z: num), returns z=a+b
  87.  
  88. A more elaborate sCOD might be
  89.  
  90.    xCOD xSAMPLE(n:num; x,y:array;  prog theSubr(a,b:num) ), a sample xCOD
  91.  
  92. which could have describe a FORTRAN routine
  93.    subroutine xSAMPLE(zN, x,y, theSubr)
  94.    extended zN
  95.    extended x(*), y(*)
  96.    external theSubr
  97.       etc.
  98.  
  99. or, in Pascal,
  100.     type
  101.        ArrayOfExtended = array[1..1] of extended;
  102.    procedure (var n:extended; x,y:ArrayOfExtended; theSubr:Ptr)
  103.    begin
  104.       etc.
  105.  
  106. NCII procedures which are passed as arguments must always be of the form
  107.     prog TheProg(n:num; zIO:array)
  108. where zIO[1…n] is an array with n components used for input/output.
  109.  
  110. Two other notes:
  111.   • NCII arguments which are undefined when passed to an xCOD are set to 0.0; therefore, they must be declared num, number, real, or extended.
  112.   • If an argument is declared complex or ComplexArray but isn't, then it is converted to complex before it is passed.  The external code may deal with this array as it likes, it is just a long array of numbers packed (r1, i1, r2, i2, ...) where "r" is the real part and "i" is the imaginary.
  113.  
  114.  
  115.   Final Remarks
  116.  
  117. This whole procedure sounds much more complicated than it really is.  However, it is true that xCOD's are fairly unforgiving.  If, for example, you pass an array of 10 numbers to an external routine but then try to treat it as if it were an array with a size of 20, then your Mac will almost certainly crash.  Also, there is no easy way to debug an xCOD once it is installed inside NCII; you should test it inside your own compiler's environment.
  118.  
  119. If you run into problems, feel free to drop me a line; I'll do what I can.
  120.  
  121. Also, if there is interest, I would be glad to collect xCOD's and their interface routines (like the ones in the Library Routines folder) and distribute them to other users.  
  122.  
  123. Good luck, 
  124.  
  125.    Jim Mahoney
  126.    PO Box 347, Marlboro, VT  05344  USA
  127.    mahoney@stars.gsfc.nasa.gov
  128.